Video game |
Video games can be created using: DirectX, OpenGL, Unity or Unreal Engine. Los video juegos pueden ser creados usando: DirectX, OpenGL, Unity o Unreal Engine. DEFINITION Bounding Shape In 3D, it is very important to detect when two objects collide. To detect collision, each object has a bounding shape that describes the shape of the object. For performance reasons, two of the most common bounding shapes are the Bounding Box and the Bounding Sphere. En 3D, es muy importante detectar cuando dos objetos chocan. Para detectar una colisión, cada objeto tiene una silueta a su alrededor que describe la forma del objeto. Por razones de desempeño, dos de las siluetas más comunes son la Caja y la Esfera. |
Tip |
Detecting collision between the objects in the scene is an expensive operation. If an object changes of position (without rotation or scaling), use the Update function of the bounding shape that only updates the position. Detectar la colisión entre los objetos en la escena es una operación costosa. Si un objeto cambia de posición (sin rotación o escalamiento) use la función Update de la bounding shape que solamente actualiza la posición. |
Problem 1 |
Create a DirectX application called FreeFall to test object collision. After creating the project, add a DirectX scene called MainScene, a DirectX object called Ball and a DirectX object called Floor. We need an image for the floor. Cree una aplicación DirectX llamada FreeFall para probar la colisión de objetos Después de crear el proyecto, agregue una escena de DirectX llamada MainScene, un objeto de DirectX llamado Ball y un objeto de DirectX llamado Floor. Vamos a necesitar una imagen para el piso. |
Ball.h |
//____________________________________________________________ Ball.h #pragma once class Ball : public DX11::Object3D { float speedY = 0.0000001f; float x = 0.0f; float y = 10.0f; float z = 0.0f; bool OnCreateScene(DX11::Engine& engine); . . . }; |
Ball.cpp |
. . . void Ball::OnUpdate(DX11::Engine& engine, float sec, float deltaSec) { if (speedY != 0.0f) { y += speedY*deltaSec; speedY -= 5.5f*deltaSec; } world = DirectX::XMMatrixTranslation(x, y, z); this->boundingShape.Update(x, y, z); } void Ball::OnRender3D(DX11::Engine& engine) { engine.shaderColorAmbient.Render(engine, world, vertexBuffer->GetIndexCount(), 0, 0); } void Ball::OnRender2D(DX11::Engine& engine) { //engine.shaderColor.RenderOrthographic(engine, vertexBuffer->GetIndexCount(), 0, 0); } void Ball::OnCollision(DX11::Engine& engine, DX11::Object3D& collisionObject) { if (speedY > 0.0f) { if (speedY > 1.0f) { speedY -= 1.0f; } else { speedY = 0.0f; } } else { if (speedY < -1.0f) { speedY += 1.0f; } else { speedY = 0.0f; } } speedY = -speedY; // Invert direction y = 1.51f; // Place the ball on the floor } |
Main.h |
//____________________________________________________________ MainScene.h #pragma once #include "Ball.h" #include "Floor.h" class MainScene : public DX11::Scene { public: DX11::Texture texture; Ball ball; Floor floor; void OnCreateScene(DX11::Engine& engine); . . . }; |
Main.cpp |
. . . void MainScene::OnCreateScene(DX11::Engine& engine) { //_______________________________________ 1. Set back color RGBA this->SetBackColor(0.0f, 0.0f, 0.2f, 1.0f); //_______________________________________ 3. Set texture floor.texture = &texture; //_______________________________________ 5. Add children this->AddChild(ball); this->AddChild(floor); //_______________________________________ 6. Camera & Light setup (light.ambientColor) camera.position.x = 0.0f; camera.position.y = 5.0f; camera.position.z = -20.0f; camera.lookAt.x = 0.0f; camera.lookAt.y = 0.0f; camera.lookAt.z = 1.0f; } . . . void MainScene::AfterCreatingChildren(DX11::Engine& engine) { ball.CreateBoundingSphere(); floor.CreateBoundingBox(); } |
FreeFall.h |
#pragma once //______________________________________ FreeFall.h #include "Resource.h" #include "MainScene.h" class FreeFall : public DX11::Window { public: MainScene mainScene; DX11::TextureNormVertexBuffer vbFloor; DX11::ColorNormVertexBuffer vbBall; }; |
FreeFall.cpp |
. . . #include "stdafx.h" //________________________________________ FreeFall.cpp #include "FreeFall.h" int APIENTRY wWinMain(. . .) { FreeFall app; //_________________________________________________________ 1. Load texture const wchar_t* error = app.mainScene.texture.CreateData(L"floor.jpg"); if (error != nullptr) { ::MessageBox(NULL, error, L"floor.bmp", MB_OK | MB_ICONERROR); return 0; } app.engine.AddChild(app.mainScene.texture); //_________________________________________________________ 2. Create vertex buffers vector<valarray<Sys::Vertex> > mesh; Sys::Tessellator tessellator; tessellator.GenerateHorizontalPlane(4, 4, 40.0f, 1.0f, 40.0f, mesh); app.vbFloor.CreateData(mesh, false); // tessellator.GenerateSphere(3, mesh); app.vbBall.CreateData(mesh, DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f), false); //_________________________________________________________ 3. Add vertex buffers to the engine app.engine.AddChild(app.vbFloor); app.engine.AddChild(app.vbBall); //_________________________________________________________ 4. Set vertex buffers to the 3D objects app.mainScene.floor.vertexBuffer = &app.vbFloor; app.mainScene.ball.vertexBuffer = &app.vbBall; //_________________________________________________________ 5. Scene setup app.engine.scene[L"mainScene"] = &app.mainScene; app.engine.SetCurrentScene(L"mainScene"); //_________________________________________________________ 6. Run the app return app.Run(. . .); } |